package com.ibo.htlivecamera.player;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.res.Configuration;
import android.graphics.ImageFormat;
import android.graphics.PixelFormat;
import android.hardware.Camera;
import android.hardware.Camera.PreviewCallback;
import android.os.Build;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v7.app.ActionBar;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.Surface;
import android.view.SurfaceHolder;
import android.view.SurfaceHolder.Callback;
import android.view.SurfaceView;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.FrameLayout;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.daniulive.smartplayer.SmartPlayerJniV2;
import com.eventhandle.NTSmartEventCallbackV2;
import com.eventhandle.NTSmartEventID;
import com.ibo.htlivecamera.R;
import com.ibo.htlivecamera.base.BaseFragment;
import com.videoengine.NTExternalRender;
import com.videoengine.NTRenderer;
import com.videoengine.NTSEIDataCallback;
import com.videoengine.NTUserDataCallback;
import com.voiceengine.NTAudioRecordV2;
import com.voiceengine.NTAudioRecordV2Callback;

import org.xutils.x;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.nio.ByteBuffer;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

public class PlayerFragment extends BaseFragment implements android.view.SurfaceHolder.Callback {


    private static final int PLAYER_EVENT_MSG = 1;
    private static final int PLAYER_USER_DATA_MSG = 2;
    private static final int PLAYER_SEI_DATA_MSG = 3;
    private static final int PORTRAIT = 1; // 竖屏
    private static final int LANDSCAPE = 2; // 横屏
    private static final String TAG = "######   ";

//    rotate_degrees += 90;
//    rotate_degrees = rotate_degrees % 360;
//
//    if (playerHandle != 0) {
//        libPlayer.SmartPlayerSetRotation(playerHandle,
//        rotate_degrees);
//    }

    private RelativeLayout rootLayout;

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View inflate = inflater.inflate(R.layout.frag_player, container, false);
        rootLayout = inflate.findViewById(R.id.root);
        return inflate;
    }

    @Override
    public void onResumeAct() {
        Log.i(TAG, "Run into activity onResume++");

        if (isPlaying && playerHandle != 0 && (!isHardwareDecoder || !is_enable_hardware_render_mode)) {
            libPlayer.SmartPlayerSetOrientation(playerHandle, currentOrigentation);
        }

        super.onResume();
    }

    @Override
    public void onDestroy() {
        if (libPlayer != null) {
            libPlayer.SmartPlayerStopPlay(playerHandle);
            libPlayer.SmartPlayerClose(playerHandle);
        }
        playerHandle = 0;
        isPlaying = false;

        super.onDestroy();

    }


    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        playbackUrl = getArguments().getString("url");
        myContext = getAct().getApplicationContext();

        log("playbackUrl = " + playbackUrl);

        libPlayer = new SmartPlayerJniV2();
        boolean bViewCreated = CreateView();
        if (bViewCreated) {
            inflateLayout(LinearLayout.VERTICAL);
        }


        InitAndSetConfig();
        //开始播放
        startPlay();
    }

    private void log(String s) {
        System.out.println("########   " + s);
    }

    Button btnStartStopPlayback;
    Button btnHardwareDecoder;
    Button btnFastStartup;
    Button btnLowLatency;
    LinearLayout lLayout = null;
    FrameLayout fFrameLayout = null;
    String encrypt_key = "";
    String encrypt_iv = "";
    private SurfaceView sSurfaceView = null;
    private long playerHandle = 0;
    private SmartPlayerJniV2 libPlayer = null;
    private int currentOrigentation = PORTRAIT;
    private String playbackUrl = "";
    private boolean isMute = false;
    private boolean isHardwareDecoder = false;
    private boolean is_enable_hardware_render_mode = false;    //设置视频硬解码下Mediacodec自行绘制模式（此种模式下，硬解码兼容性和效率更好，回调YUV/RGB和快照功能将不可用）
    private int playBuffer = 200; // 默认200ms
    private boolean isLowLatency = false; // 超低延时，默认不开启
    private boolean isFastStartup = true; // 是否秒开, 默认true
    private int rotate_degrees = 90;
    private boolean isPlaying = false;
    private Context myContext;


    public void surfaceChanged(SurfaceHolder holder, int format, int in_width, int in_height) {
    }

    public void surfaceCreated(SurfaceHolder holder) {
        if (isHardwareDecoder && is_enable_hardware_render_mode && isPlaying) {
            libPlayer.SmartPlayerUpdateHWRenderSurface(playerHandle);
        }
    }

    public void surfaceDestroyed(SurfaceHolder holder) {
        Log.i(TAG, "surfaceDestroyed..");
    }


    /* Generate basic layout */
    private void inflateLayout(int orientation) {
        if (null == lLayout)
            lLayout = new LinearLayout(getContext());

        rootLayout.addView(lLayout, new android.view.ViewGroup.LayoutParams(android.view.ViewGroup.LayoutParams.WRAP_CONTENT, android.view.ViewGroup.LayoutParams.WRAP_CONTENT));

        lLayout.setOrientation(orientation);

        fFrameLayout = new FrameLayout(getContext());

        ViewGroup.LayoutParams lp = new LinearLayout.LayoutParams(
                ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT, 1.0f);
        fFrameLayout.setLayoutParams(lp);
        Log.i(TAG, "++inflateLayout..");

        sSurfaceView.setLayoutParams(new ViewGroup.LayoutParams(
                ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));

        fFrameLayout.addView(sSurfaceView, 0);

        RelativeLayout outLinearLayout = new RelativeLayout(getContext());
        outLinearLayout.setLayoutParams(new ViewGroup.LayoutParams(
                ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.FILL_PARENT));

        LinearLayout lLinearLayout = new LinearLayout(getContext());
        lLinearLayout.setOrientation(LinearLayout.VERTICAL);
        lLinearLayout.setLayoutParams(new ViewGroup.LayoutParams(
                ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT));

        LinearLayout copyRightLinearLayout = new LinearLayout(getContext());
        copyRightLinearLayout.setOrientation(LinearLayout.VERTICAL);
        RelativeLayout.LayoutParams rl = new RelativeLayout.LayoutParams(
                ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        rl.topMargin = getAct().getWindowManager().getDefaultDisplay().getHeight() - 270;
        copyRightLinearLayout.setLayoutParams(rl);

        LinearLayout LinearLayoutSwitchUrl = new LinearLayout(getContext());
        LinearLayoutSwitchUrl.setOrientation(LinearLayout.HORIZONTAL);
        LinearLayoutSwitchUrl.setLayoutParams(new ViewGroup.LayoutParams(
                ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT));

        lLinearLayout.addView(LinearLayoutSwitchUrl);

        /* hardware decoder button */
        btnHardwareDecoder = new Button(getContext());

        if (!isHardwareDecoder) {
            btnHardwareDecoder.setText("当前软解码");
        } else {
            btnHardwareDecoder.setText("当前硬解码");
        }

        LinearLayout LinearLayoutImage = new LinearLayout(getContext());
        LinearLayoutImage.setOrientation(LinearLayout.HORIZONTAL);
        LinearLayoutImage.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT));

//        LinearLayoutImage.addView(btnRotation);
        lLinearLayout.addView(LinearLayoutImage);

        btnHardwareDecoder.setLayoutParams(new ViewGroup.LayoutParams(
                ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT));
//        lLinearLayout.addView(btnHardwareDecoder);

        // buffer setting++

        LinearLayout bufferLinearLayout = new LinearLayout(getContext());
        bufferLinearLayout.setOrientation(LinearLayout.HORIZONTAL);
        bufferLinearLayout.setLayoutParams(new ViewGroup.LayoutParams(
                ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT));

        btnLowLatency = new Button(getContext());

        if (isLowLatency) {
            btnLowLatency.setText("当前超低延时");
        } else {
            btnLowLatency.setText("当前正常延时");
        }

        btnLowLatency.setLayoutParams(new ViewGroup.LayoutParams(
                ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT));
//        bufferLinearLayout.addView(btnLowLatency);

        btnFastStartup = new Button(getContext());

        if (isFastStartup) {
            btnFastStartup.setText("停用秒开");
        } else {
            btnFastStartup.setText("启用秒开");
        }

        btnFastStartup.setLayoutParams(new ViewGroup.LayoutParams(
                ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT));
//        bufferLinearLayout.addView(btnFastStartup);

//        lLinearLayout.addView(bufferLinearLayout);

        // buffer setting--

        /* Start playback stream button */
        btnStartStopPlayback = new Button(getContext());
        btnStartStopPlayback.setText("开始播放 ");
        btnStartStopPlayback.setLayoutParams(new ViewGroup.LayoutParams(
                ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT));
//        lLinearLayout.addView(btnStartStopPlayback);

        outLinearLayout.addView(lLinearLayout, 0);
        outLinearLayout.addView(copyRightLinearLayout, 1);
        fFrameLayout.addView(outLinearLayout, 1);

        lLayout.addView(fFrameLayout, 0);

        if (isPlaying) {
            btnHardwareDecoder.setEnabled(false);

            btnLowLatency.setEnabled(false);
            btnFastStartup.setEnabled(false);
        } else {
            btnHardwareDecoder.setEnabled(true);

            btnLowLatency.setEnabled(true);
            btnFastStartup.setEnabled(true);
        }
        if (isPlaying) {
            btnStartStopPlayback.setText("停止播放 ");
        } else {
            btnStartStopPlayback.setText("开始播放 ");
        }
        btnHardwareDecoder.setOnClickListener(new Button.OnClickListener() {
            public void onClick(View v) {
                isHardwareDecoder = !isHardwareDecoder;
                if (isHardwareDecoder) {
                    btnHardwareDecoder.setText("当前硬解码");
                } else {
                    btnHardwareDecoder.setText("当前软解码");
                }

            }
        });


        btnLowLatency.setOnClickListener(new Button.OnClickListener() {
            public void onClick(View v) {
                isLowLatency = !isLowLatency;

                if (isLowLatency) {
                    playBuffer = 0;
                    Log.i(TAG, "low latency mode, set playBuffer to 0");
                    btnLowLatency.setText("当前超低延时");
                } else {
                    btnLowLatency.setText("当前正常延时");
                }
            }
        });

        btnFastStartup.setOnClickListener(new Button.OnClickListener() {
            public void onClick(View v) {
                isFastStartup = !isFastStartup;

                if (isFastStartup) {
                    btnFastStartup.setText("停用秒开");
                } else {
                    btnFastStartup.setText("启用秒开");
                }
            }
        });

        btnStartStopPlayback.setOnClickListener(new Button.OnClickListener() {
            // @Override
            public void onClick(View v) {
                if (isPlaying) {
                    stopPlayer();
                } else {
                    startPlay();
                }
            }
        });
    }

    private void startPlay() {
        Log.i(TAG, "开始播放" + playbackUrl);

        // 如果第二个参数设置为null，则播放纯音频
        libPlayer.SmartPlayerSetSurface(playerHandle, sSurfaceView);

        if (isHardwareDecoder && is_enable_hardware_render_mode) {
            libPlayer.SmartPlayerSetHWRenderMode(playerHandle, 1);
        }

        // External Render test
        // libPlayer.SmartPlayerSetExternalRender(playerHandle, new
        // RGBAExternalRender());
        // libPlayer.SmartPlayerSetExternalRender(playerHandle, new
        // I420ExternalRender());

        libPlayer.SmartPlayerSetUserDataCallback(playerHandle, new UserDataCallback());
        //libPlayer.SmartPlayerSetSEIDataCallback(playerHandle, new SEIDataCallback());

        libPlayer.SmartPlayerSetAudioOutputType(playerHandle, 0);

        if (isMute) {
            libPlayer.SmartPlayerSetMute(playerHandle, isMute ? 1 : 0);
        }

        if (isHardwareDecoder) {
            int isSupportHevcHwDecoder = libPlayer.SetSmartPlayerVideoHevcHWDecoder(playerHandle, 1);

            int isSupportH264HwDecoder = libPlayer
                    .SetSmartPlayerVideoHWDecoder(playerHandle, 1);

            Log.i(TAG, "isSupportH264HwDecoder: " + isSupportH264HwDecoder + ", isSupportHevcHwDecoder: " + isSupportHevcHwDecoder);
        }

        libPlayer.SmartPlayerSetLowLatencyMode(playerHandle, isLowLatency ? 1
                : 0);

        libPlayer.SmartPlayerSetFlipVertical(playerHandle, 1);

        libPlayer.SmartPlayerSetFlipHorizontal(playerHandle, 0);

        libPlayer.SmartPlayerSetRotation(playerHandle, rotate_degrees);

        int iPlaybackRet = libPlayer.SmartPlayerStartPlay(playerHandle);

        if (iPlaybackRet != 0) {
            Log.e(TAG, "Call SmartPlayerStartPlay failed..");
            return;
        }

        btnStartStopPlayback.setText("停止播放 ");

        btnHardwareDecoder.setEnabled(false);
        btnLowLatency.setEnabled(false);
        btnFastStartup.setEnabled(false);

        isPlaying = true;
        Log.i(TAG, "Start playback stream--");
    }

    private void stopPlayer() {
        Log.i(TAG, "Stop playback stream++");
        int iRet = libPlayer.SmartPlayerStopPlay(playerHandle);
        if (iRet != 0) {
            Log.e(TAG, "Call SmartPlayerStopPlay failed..");
            return;
        }
        btnHardwareDecoder.setEnabled(true);
        btnLowLatency.setEnabled(true);
        isPlaying = false;
        btnStartStopPlayback.setText("开始播放 ");
        if (is_enable_hardware_render_mode && sSurfaceView != null) {
            sSurfaceView.setVisibility(View.GONE);
            sSurfaceView.setVisibility(View.VISIBLE);
        }
        Log.i(TAG, "Stop playback stream--");
    }

    /* Create rendering */
    private boolean CreateView() {

        if (sSurfaceView == null) {

            Log.i(TAG, "CreateView..");

            String manufacturer = Build.MANUFACTURER;
            Log.i(TAG, "CreateView, current manufacturer: " + manufacturer);

            if (is_enable_hardware_render_mode) {
                //hardware render模式，第二个参数设置为false
                sSurfaceView = NTRenderer.CreateRenderer(getContext(), false);
            } else {
                //这个字符串可以自己定义,例如判断华为就填写huawei,魅族就填写meizu
                if ("huawei".equalsIgnoreCase(manufacturer)) {
                    sSurfaceView = NTRenderer.CreateRenderer(getContext(), true);
                } else {
                    /*
                     * useOpenGLES2: If with true: Check if system supports openGLES, if
                     * supported, it will choose openGLES. If with false: it will set
                     * with default surfaceView;
                     */
                    sSurfaceView = NTRenderer.CreateRenderer(getContext(), true);
                }
            }
        }

        if (sSurfaceView == null) {
            Log.i(TAG, "Create render failed..");
            return false;
        }

        if (is_enable_hardware_render_mode) {
            SurfaceHolder surfaceHolder = sSurfaceView.getHolder();
            if (surfaceHolder == null) {
                Log.e(TAG, "CreateView, surfaceHolder with null..");
            }
            surfaceHolder.addCallback(this);
        }

        return true;
    }

    @Override
    public void onConfigurationChanged(Configuration newConfig) {
        super.onConfigurationChanged(newConfig);

        Log.i(TAG, "Run into onConfigurationChanged++");

        if (null != fFrameLayout) {
            fFrameLayout.removeAllViews();
            fFrameLayout = null;
        }

        if (null != lLayout) {
            lLayout.removeAllViews();
            lLayout = null;
        }

        if (newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE) {
            Log.i(TAG, "onConfigurationChanged, with LANDSCAPE。。");

            inflateLayout(LinearLayout.HORIZONTAL);

            currentOrigentation = PORTRAIT;
        } else {
            Log.i(TAG, "onConfigurationChanged, with PORTRAIT。。");

            inflateLayout(LinearLayout.VERTICAL);

            currentOrigentation = PORTRAIT;
        }

        if (!isPlaying)
            return;

        if (!isHardwareDecoder || !is_enable_hardware_render_mode) {
            libPlayer.SmartPlayerSetOrientation(playerHandle, currentOrigentation);
        }

        Log.i(TAG, "Run out of onConfigurationChanged--");
    }


    private void InitAndSetConfig() {
        playerHandle = libPlayer.SmartPlayerOpen(myContext);

        if (playerHandle == 0) {
            Log.e(TAG, "surfaceHandle with nil..");
            return;
        }

        libPlayer.SetSmartPlayerEventCallbackV2(playerHandle, new EventHandeV2());

        libPlayer.SmartPlayerSetBuffer(playerHandle, playBuffer);

        // set report download speed(默认2秒一次回调 用户可自行调整report间隔)
        libPlayer.SmartPlayerSetReportDownloadSpeed(playerHandle, 1, 2);

        libPlayer.SmartPlayerSetFastStartup(playerHandle, isFastStartup ? 1 : 0);

        //设置RTSP超时时间
        int rtsp_timeout = 10;
        libPlayer.SmartPlayerSetRTSPTimeout(playerHandle, rtsp_timeout);

        //设置RTSP TCP/UDP模式自动切换
        int is_auto_switch_tcp_udp = 1;
        libPlayer.SmartPlayerSetRTSPAutoSwitchTcpUdp(playerHandle, is_auto_switch_tcp_udp);

        libPlayer.SmartPlayerSaveImageFlag(playerHandle, 1);

        // It only used when playback RTSP stream..
        libPlayer.SmartPlayerSetRTSPTcpMode(playerHandle, 1);

        //playbackUrl = "rtmp://live.hkstv.hk.lxdns.com/live/hks2";

        //playbackUrl = "rtsp://184.72.239.149/vod/mp4://BigBuckBunny_175k.mov";

        if (playbackUrl == null) {
            Log.e(TAG, "playback URL with NULL...");
            return;
        }

        libPlayer.SmartPlayerSetUrl(playerHandle, playbackUrl);

        if (encrypt_key != null && !encrypt_key.isEmpty()) {
            Log.i(TAG, "encrypt_key:" + encrypt_key);

            int key_len = 16;

            if (encrypt_key.length() > 16 && encrypt_key.length() <= 24) {
                key_len = 24;
            } else if (encrypt_key.length() > 24) {
                key_len = 32;
            }

            byte[] key = new byte[key_len];

            for (int i = 0; i < key_len; i++) {
                key[i] = 0;
            }

            try {
                byte[] key_utf8 = encrypt_key.getBytes("UTF-8");

                int copy_key_len = key_utf8.length < key_len ? key_utf8.length : key_len;

                for (int i = 0; i < copy_key_len; ++i) {
                    key[i] = key_utf8[i];
                }

            } catch (UnsupportedEncodingException e) {
                e.printStackTrace();
            }

            libPlayer.SmartPlayerSetKey(playerHandle, key, key.length);

            if (encrypt_iv != null && !encrypt_iv.isEmpty()) {
                Log.i(TAG, "encrypt_iv:" + encrypt_iv);

                int iv_len = 16;
                byte[] iv = new byte[iv_len];

                for (int i = 0; i < iv_len; i++) {
                    iv[i] = 0;
                }

                try {
                    byte[] iv_utf8 = encrypt_iv.getBytes("UTF-8");

                    int copy_iv_len = iv_utf8.length < key_len ? iv_utf8.length : key_len;

                    for (int i = 0; i < copy_iv_len; ++i) {
                        iv[i] = iv_utf8[i];
                    }

                } catch (UnsupportedEncodingException e) {
                    e.printStackTrace();
                }

                libPlayer.SmartPlayerSetDecryptionIV(playerHandle, iv, iv.length);
            }
        }
    }

    class UserDataCallback implements NTUserDataCallback {
        private static final int NT_SDK_E_H264_SEI_USER_DATA_TYPE_BYTE_DATA = 1;
        private static final int NT_SDK_E_H264_SEI_USER_DATA_TYPE_UTF8_STRING = 2;
        private int user_data_buffer_size = 0;
        private ByteBuffer user_data_buffer_ = null;

        @Override
        public ByteBuffer getUserDataByteBuffer(int size) {
            if (size < 1) {
                return null;
            }

            if (size <= user_data_buffer_size && user_data_buffer_ != null) {
                return user_data_buffer_;
            }

            user_data_buffer_size = size + 512;
            user_data_buffer_ = ByteBuffer.allocateDirect(user_data_buffer_size);

            return user_data_buffer_;
        }

        private String byteArrayToStr(byte[] byteArray) {
            if (byteArray == null) {
                return null;
            }
            String str = new String(byteArray);
            return str;
        }

        public void onUserDataCallback(int ret, int data_type, int size, long timestamp, long reserve1, long reserve2) {
            //Log.i("onUserDataCallback", "ret: " + ret + ", data_type: " + data_type + ", size: " + size + ", timestamp: "+ timestamp);
            if (data_type == NT_SDK_E_H264_SEI_USER_DATA_TYPE_UTF8_STRING) {
                if (user_data_buffer_ == null)
                    return;

                user_data_buffer_.rewind();

                byte[] byte_buffer = new byte[size];
                user_data_buffer_.get(byte_buffer);

                String str = byteArrayToStr(byte_buffer);

                Log.i(TAG, "onUserDataCallback, userdata: " + str);
            }
        }
    }

    class EventHandeV2 implements NTSmartEventCallbackV2 {
        @Override
        public void onNTSmartEventCallbackV2(long handle, int id, long param1,
                                             long param2, String param3, String param4, Object param5) {

            String player_event = "";

            switch (id) {
                case NTSmartEventID.EVENT_DANIULIVE_ERC_PLAYER_STARTED:
                    player_event = "开始..";
                    break;
                case NTSmartEventID.EVENT_DANIULIVE_ERC_PLAYER_CONNECTING:
                    player_event = "连接中..";
                    break;
                case NTSmartEventID.EVENT_DANIULIVE_ERC_PLAYER_CONNECTION_FAILED:
                    player_event = "连接失败..";
                    break;
                case NTSmartEventID.EVENT_DANIULIVE_ERC_PLAYER_CONNECTED:
                    player_event = "连接成功..";
                    break;
                case NTSmartEventID.EVENT_DANIULIVE_ERC_PLAYER_DISCONNECTED:
                    player_event = "连接断开..";
                    break;
                case NTSmartEventID.EVENT_DANIULIVE_ERC_PLAYER_STOP:
                    player_event = "停止播放..";
                    break;
                case NTSmartEventID.EVENT_DANIULIVE_ERC_PLAYER_RESOLUTION_INFO:
                    player_event = "分辨率信息: width: " + param1 + ", height: " + param2;
                    break;
                case NTSmartEventID.EVENT_DANIULIVE_ERC_PLAYER_NO_MEDIADATA_RECEIVED:
                    player_event = "收不到媒体数据，可能是url错误..";
                    break;
                case NTSmartEventID.EVENT_DANIULIVE_ERC_PLAYER_SWITCH_URL:
                    player_event = "切换播放URL..";
                    break;
                case NTSmartEventID.EVENT_DANIULIVE_ERC_PLAYER_CAPTURE_IMAGE:
                    player_event = "快照: " + param1 + " 路径：" + param3;

                    if (param1 == 0) {
                        player_event = player_event + ", 截取快照成功";
                    } else {
                        player_event = player_event + ", 截取快照失败";
                    }
                    break;

                case NTSmartEventID.EVENT_DANIULIVE_ERC_PLAYER_RECORDER_START_NEW_FILE:
                    player_event = "[record]开始一个新的录像文件 : " + param3;
                    break;
                case NTSmartEventID.EVENT_DANIULIVE_ERC_PLAYER_ONE_RECORDER_FILE_FINISHED:
                    player_event = "[record]已生成一个录像文件 : " + param3;
                    break;

                case NTSmartEventID.EVENT_DANIULIVE_ERC_PLAYER_START_BUFFERING:
                    Log.i(TAG, "Start Buffering");
                    break;

                case NTSmartEventID.EVENT_DANIULIVE_ERC_PLAYER_BUFFERING:
                    Log.i(TAG, "Buffering:" + param1 + "%");
                    break;

                case NTSmartEventID.EVENT_DANIULIVE_ERC_PLAYER_STOP_BUFFERING:
                    Log.i(TAG, "Stop Buffering");
                    break;

                case NTSmartEventID.EVENT_DANIULIVE_ERC_PLAYER_DOWNLOAD_SPEED:
//                    player_event = "download_speed:" +  "KB/s";
                    break;

                case NTSmartEventID.EVENT_DANIULIVE_ERC_PLAYER_RTSP_STATUS_CODE:
                    Log.e(TAG, "RTSP error code received, please make sure username/password is correct, error code:" + param1);
                    player_event = "RTSP error code:" + param1;
                    break;

                case NTSmartEventID.EVENT_DANIULIVE_ERC_PLAYER_NEED_KEY:
                    Log.e(TAG, "RTMP加密流，请设置播放需要的Key..");
                    player_event = "RTMP加密流，请设置播放需要的Key..";
                    break;

                case NTSmartEventID.EVENT_DANIULIVE_ERC_PLAYER_KEY_ERROR:
                    Log.e(TAG, "RTMP加密流，Key错误，请重新设置..");
                    player_event = "RTMP加密流，Key错误，请重新设置..";
                    break;
            }
            log(player_event);

        }
    }


}
